package com.yuanxiao.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
@Activate(group = {CommonConstants.PROVIDER})
public class TPMoniterFilter implements Filter {
    private final static double TP90 = 90.0;
    private final static double TP99 = 99.0;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long startTime = System.currentTimeMillis();
        try {
            return invoker.invoke(invocation);
        } finally {
            long endTime = System.currentTimeMillis();
            long res = endTime - startTime;
            double sumTime = res*1.0 ;

            String methodName = invocation.getMethodName();
            if (sumTime >= TP90 && sumTime < TP99) {
                //放到队列TP90
                TPMap.getTP90Map(methodName).add(sumTime);

            }
            if (sumTime >= TP99) {
                //放到队列TP99
                TPMap.getTP99Map(methodName).add(sumTime);
            }
        }
    }
}
